package com.sphd.education.activitys;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.sphd.education.R;
import com.sphd.education.fragments.HomeSearchFragment;
import com.sphd.education.fragments.SearchLessonFragment;
import com.sphd.education.fragments.SearchSchoolFragment;
import com.sphd.education.info.EducationIntentKey;
import com.sphd.education.info.KeyWordInfo;
import com.sphd.education.interfaces.SendSearchInterface;

import butterknife.Bind;
import butterknife.ButterKnife;
/**
 *主界面的搜索
 */

public class HomeSerachActivity extends AppCompatActivity implements SendSearchInterface {

    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.edit_tv)
    EditText editText;
    int type=1;
    boolean isShowing=false;
    private Bundle bundle;
    HomeSearchFragment homeSearchFragment;
    SearchLessonFragment lessonFragment;
    SearchSchoolFragment schoolFragment;
    KeyWordInfo keyWordInfo;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_serach);
        ButterKnife.bind(this);
        initeView();
    }



    private void initeView() {
        toolbar.setTitle("搜索课程或学校");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();//这是自带的箭头的返回的监听
            }
        });
        bundle = new Bundle();
        keyWordInfo=new KeyWordInfo();
        addData(0);
    }

    private void addData(int i) {
        FragmentManager fragmentManager =getSupportFragmentManager();
        FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
        hideFragemt(fragmentTransaction);
        switch (i){
            case 0:
                homeSearchFragment =HomeSearchFragment.newInstance(bundle);
                fragmentTransaction.replace(R.id.my_fragment, homeSearchFragment);
                homeSearchFragment.setSendCallBack(this);
                break;
            case 2:
                lessonFragment=SearchLessonFragment.newInstance(bundle);
                fragmentTransaction.replace(R.id.my_fragment, lessonFragment);
                Log.e("bundle2",bundle.getString("condition"));

                break;
            case 1:
                schoolFragment=SearchSchoolFragment.newInstance(bundle);
                fragmentTransaction.replace(R.id.my_fragment, schoolFragment);
                break;
        }
        fragmentTransaction.commit();
    }

    private void hideFragemt(FragmentTransaction transaction) {
        if (lessonFragment !=null){
            transaction.hide(lessonFragment);
        }
        if (schoolFragment !=null){
            transaction.hide(schoolFragment);
        }
        if (homeSearchFragment!=null){
            transaction.hide(homeSearchFragment);
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_search_type, menu);
        return super.onCreateOptionsMenu(menu);
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id==R.id.menu_cheange_type){
            if (isShowing) {
                if (type == 2) {
                    editText.setHint("找学校");
                    editText.setText("");
                    type = 1;
                } else {
                    editText.setHint("找课程");
                    editText.setText("");
                    type = 2;
                }
            }
        }
        if (id == R.id.menu_search_btn) {
            Log.e("menu_search_btn","menu_search_btn");
            if (isShowing){
                editText.setVisibility(View.GONE);
                isShowing=false;
                if (type==2){
                    bundle.putString(EducationIntentKey.FLAG,EducationIntentKey.SEARCH_LESSON_TAG);
                    bundle.putString("condition",editText.getText().toString());
                    keyWordInfo.setWord(editText.getText().toString());
                    keyWordInfo.setType(2);
                    keyWordInfo.save();
                    editText.setText("");
                    addData(2);
                    Log.e("TV",editText.getText().toString());
                }
                if (type==1){
                    bundle.putString(EducationIntentKey.FLAG,EducationIntentKey.SEARCH_SCHOOL_TAG);
                    bundle.putString("condition",editText.getText().toString());
                    keyWordInfo.setWord(editText.getText().toString());
                    keyWordInfo.setType(1);
                    keyWordInfo.save();
                    editText.setText("");
                    addData(1);
                }

            }else {
                editText.setVisibility(View.VISIBLE);
                isShowing=true;
                Log.e("else","else");
            }

            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void SendSearchCallBack(int p, String s) {
        if (p==2){
            bundle.putString(EducationIntentKey.FLAG, EducationIntentKey.SEARCH_LESSON_TAG);
            bundle.putString("condition", s);
            addData(2);
            Log.e("TV",editText.getText().toString());
        }
        if (p==1){
            bundle.putString(EducationIntentKey.FLAG,EducationIntentKey.SEARCH_SCHOOL_TAG);
            bundle.putString("condition",s);
            addData(1);
        }
    }
}
